home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Mp3 / MPEGAudioPlayerSource.cpt / map / header.cc < prev    next >
Text File  |  1994-10-05  |  7KB  |  253 lines

  1. /*
  2.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  *  Changes from version 1.1 to 1.2:
  23.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  24.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  25.  *    - syncword recognition slightly changed
  26.  */
  27.  
  28. //#include <sys/types.h>
  29. //#include <unistd.h>
  30. #include <iostream.h>
  31. //#include <iomanip.h>
  32. #include <stdlib.h>
  33. #include "header.h"
  34.  
  35.  
  36. const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };
  37.  
  38.  
  39. bool Header::read_header (Ibitstream *stream, Crc16 **crcp)
  40. {
  41.   uint32 headerstring;
  42.  
  43.   if (!stream->get_header (&headerstring))
  44.     return False;
  45.  
  46.   if ((headerstring & 0xFFF80000) != 0xFFF80000)
  47.   {
  48.       long pos;
  49.       GetFPos(stream->filedescriptor(), &pos);
  50.       
  51.     cerr << "invalid syncword 0x";
  52.     cerr.width (8);
  53.     cerr.fill ('0');
  54.     cerr << hex << headerstring
  55.      << " found at fileoffset " << dec
  56.      << pos - 4 << '\n';
  57.     return False;
  58.   }
  59.  
  60.   if ((h_layer = (headerstring >> 17) & 3) == 0)
  61.   {
  62.     cerr << "unknown layer identifier found!\n";
  63.     exit (1);
  64.   }
  65.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III
  66.   if (h_layer == 3)
  67.   {
  68.     cerr << "Sorry, Layer III not implemented!\n";
  69.     exit (1);
  70.   }
  71.   h_protection_bit = (headerstring >> 16) & 1;
  72.   if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  73.   {
  74.     cerr << "unknown bitrate index found!\n";
  75.     exit (1);
  76.   }
  77.   if (!h_bitrate_index)
  78.   {
  79.     cerr << "free format not yet implemented!\n";
  80.     exit (1);
  81.   }
  82.  
  83.   if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  84.   {
  85.     cerr << "unknown sample frequency!\n";
  86.     exit (1);
  87.   }
  88.   h_padding_bit = (headerstring >> 9) & 1;
  89.   h_mode = (e_mode)((headerstring >> 6) & 3);
  90.   if (h_layer == 2)
  91.     // testing validity of mode and bitrate:
  92.     if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  93.      h_mode != single_channel) ||
  94.     (h_bitrate_index >= 11 && h_mode == single_channel))
  95.     {
  96.       cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  97.           "  mode: " << mode_string ()
  98.        << "\n  bitrate: " << bitrate_string () << '\n';
  99.       exit (1);
  100.     }
  101.   h_mode_extension = (headerstring >> 4) & 3;
  102.   if (h_mode == joint_stereo)
  103.     h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  104.   else
  105.     h_intensity_stereo_bound = 0;        // should never be used
  106.   h_copyright = (headerstring >> 3) & 1;
  107.   h_original = (headerstring >> 2) & 1;
  108.  
  109.   // calculate number of subbands:
  110.   if (h_layer == 1)
  111.     h_number_of_subbands = 32;
  112.   else
  113.   {
  114.     uint32 channel_bitrate = h_bitrate_index;
  115.  
  116.     // calculate bitrate per channel:
  117.     if (h_mode != single_channel)
  118.       if (channel_bitrate == 4)
  119.     channel_bitrate = 1;
  120.       else
  121.     channel_bitrate -= 4;
  122.  
  123.     if (channel_bitrate == 1 || channel_bitrate == 2)
  124.       if (h_sample_frequency == thirtytwo)
  125.     h_number_of_subbands = 12;
  126.       else
  127.     h_number_of_subbands = 8;
  128.     else
  129.       if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
  130.     h_number_of_subbands = 27;
  131.       else
  132.     h_number_of_subbands = 30;
  133.   }
  134.  
  135.   if (h_intensity_stereo_bound > h_number_of_subbands)
  136.     h_intensity_stereo_bound = h_number_of_subbands;
  137.  
  138.   // read framedata:
  139.   if (!stream->read_frame (calculate_framesize ()))
  140.     return False;
  141.  
  142.   if (!h_protection_bit)
  143.   {
  144.     // frame contains a crc checksum
  145.     checksum = (uint16)stream->get_bits (16);
  146.     if (!crc)
  147.       crc = new Crc16;
  148.     crc->add_bits (headerstring, 16);
  149.     *crcp = crc;
  150.   }
  151.   else
  152.     *crcp = (Crc16 *)0;
  153.  
  154.   return True;
  155. }
  156.  
  157.  
  158. uint32 Header::calculate_framesize ()
  159. /* calculates framesize in bytes excluding header size */
  160. {
  161.   static const int32 bitrates_layer_1[15] = {
  162.     0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  163.     224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000 };
  164.   static const int32 bitrates_layer_2[15] = {
  165.     0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  166.     112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000 };
  167.   static const samplefrequencies[3] = { 44100, 48000, 32000 };
  168.   uint32 framesize;
  169.  
  170.   if (h_layer == 1)
  171.   {
  172.     framesize = (12 * bitrates_layer_1[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  173.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  174.       ++framesize;
  175.     framesize <<= 2;        // one slot is 4 bytes long
  176.   }
  177.   else
  178.   {
  179.     framesize = (144 * bitrates_layer_2[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  180.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  181.       ++framesize;
  182.   }
  183.  
  184.   return framesize - 4;        // subtract header size
  185. }
  186.  
  187.  
  188. const char *Header::layer_string (void)
  189. {
  190.   switch (h_layer)
  191.   {
  192.     case 1:
  193.       return "I";
  194.     case 2:
  195.       return "II";
  196.     case 3:
  197.       return "III (not implemented!)";
  198.   }
  199.   return NULL;            // dummy
  200. }
  201.  
  202.  
  203. const char *Header::bitrate_string (void)
  204. {
  205.   static const char *layer1_bitrates[16] = {
  206.     "free format", "32 kbis/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s", 
  207.     "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
  208.     "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"
  209.   };
  210.   static const char *layer2_bitrates[16] = {
  211.     "free format", "32 kbis/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", 
  212.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
  213.     "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"
  214.   };
  215.  
  216.   if (h_layer == 1)
  217.     return layer1_bitrates[h_bitrate_index];
  218.   else
  219.     return layer2_bitrates[h_bitrate_index];
  220. }
  221.  
  222.  
  223. const char *Header::sample_frequency_string (void)
  224. {
  225.   switch (h_sample_frequency)
  226.   {
  227.     case thirtytwo:
  228.       return "32 kHz";
  229.     case fourtyfour_point_one:
  230.       return "44.1 kHz";
  231.     case fourtyeight:
  232.       return "48 kHz";
  233.   }
  234.   return NULL;            // dummy
  235. }
  236.  
  237.  
  238. const char *Header::mode_string (void)
  239. {
  240.   switch (h_mode)
  241.   {
  242.     case stereo:
  243.       return "stereo";
  244.     case joint_stereo:
  245.       return "joint stereo";
  246.     case dual_channel:
  247.       return "dual channel";
  248.     case single_channel:
  249.       return "single channel";
  250.   }
  251.   return NULL;            // dummy
  252. }
  253.